//
//  RegistrationViewController.swift
//  Link
//
//  Created by melvyn on 2018/8/23.
//  Copyright © 2018 NEET. All rights reserved.
//

import UIKit
#if canImport(RxSwift)
import RxSwift
import RxCocoa
#endif

class RegistrationViewController: UIViewController {
    
    @IBOutlet weak var dismissButton: UIButton!
    @IBOutlet weak var usernameField: UITextField!
    @IBOutlet weak var pwdField: UITextField!
    @IBOutlet weak var pwdVerifyField: UITextField!
    @IBOutlet weak var signupButton: UIButton!
    
    private let disposeBag = DisposeBag()
    private var viewModel:RegistrationViewModel!
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    convenience init() {
        let (nibName, bundle) = Assets.Nib.registration()
        self.init(nibName: nibName, bundle: bundle)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        setupTextFields()

        pwdVerifyField.leftView?.tintColor = .white
        pwdField.leftView?.tintColor = .white
        usernameField.leftView?.tintColor = .white
        
        #if canImport(RxSwift)
        viewModel = RegistrationViewModel.init((
            usernameField.rx.text.asObservable().ignoreNil(),
            pwdField.rx.text.asObservable().ignoreNil(),
            pwdVerifyField.rx.text.asObservable().ignoreNil()
        ))
        
        signupButton.rx.tap
            .bind(to: viewModel.signupTap)
            .disposed(by: disposeBag)
        
        dismissButton.rx.tap
            .bind(onNext: { [unowned self] _ in
                self.presentingViewController?.dismiss(animated: true, completion: nil)
            })
            .disposed(by: disposeBag)
        
        viewModel.isEnabled
            .drive(signupButton.rx.isEnabled)
            .disposed(by: disposeBag)
        
        viewModel.registration
            .filter({ $0.value == true })
            .drive(onNext: { [unowned self] _ in
                self.presentingViewController?.dismiss(animated: true, completion: nil)
            })
            .disposed(by: disposeBag)
        
        #else
        signupButton.addTarget(viewModel, action: #selector(RegistrationViewModel.signupEventHandler), for: .touchUpInside)
        
        dismissButton.addTarget(self, action: #selector(RegistrationViewController._dismiss), for: .touchUpInside)
        
        viewModel.registration = { [weak self] in
            guard $0.value == true else {
                return
            }
            self?.presentingViewController?.dismiss(animated: true, completion: nil)
        }
        
        viewModel.isEnabled = { [weak self] isEnabled in
            self?.signupButton.isEnabled = isEnabled
        }
        #endif
    }
    
    private func setupTextFields() {
        usernameField.leftView = UIImageView(image: Assets.Image.username())
        usernameField.leftViewMode = .always
        
        pwdField.leftView = UIImageView(image: Assets.Image.password())
        pwdField.leftViewMode = .always
        
        pwdVerifyField.leftView = UIImageView(image: Assets.Image.password())
        pwdVerifyField.leftViewMode = .always
    }
    
    #if !canImport(RxSwift)
    @objc private func _dismiss() {
        presentingViewController?.dismiss(animated: true, completion: nil)
    }
    #endif
}
